import 'dart:async';

import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';

class PhoneRegistoryWidget extends StatefulWidget {
  //Function onTimerFinish;

  //PhoneRegistoryWidget({this.onTimerFinish}) : super();

  @override
  _PhoneRegistoryWidgetState createState() => _PhoneRegistoryWidgetState();
}

class _PhoneRegistoryWidgetState extends State<PhoneRegistoryWidget> {
  //声明一个全局key

  GlobalKey _key = GlobalKey<FormState>();

  TextEditingController _phoneCode = TextEditingController();

  FocusNode _code = FocusNode();

  late Timer _timer;
  int _countdownTime = 0;

  @override
  Widget build(BuildContext context) {
    return Padding(
        padding: EdgeInsets.only(left: 20,right: 20,),
        child: Container(
          decoration: BoxDecoration(color: Colors.white),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Expanded(
                child: Text(
                  "手机号码",
                  style: TextStyle(fontSize: 16),
                ),
              ),
              Expanded(
                child: TextFormField(
                  cursorWidth: 1,
                  controller: _phoneCode,
                  //装饰器
                  decoration: InputDecoration(
                      hintText: "请输入手机号",
                      border: InputBorder.none,
                      hintTextDirection: TextDirection.ltr),
                  validator: (v) {
                    if (v == null || v.length < 5) {
                      return "账号小于5位";
                    }
                  },
                  //focusNode: _code,
                  //autofocus: true,
                ),
              ),
              GestureDetector(
                onTap: () {
                  if(_phoneCode.text.toString().length < 5){
                    BrnToast.show("手机号不能为空", context);
                    return;
                  }
                  if (_countdownTime == 0) {
                    setState(() {
                      _countdownTime = 5;
                    });
                    //发送请求,,,,,
                    print("点击发送请求获取验证码..."+_phoneCode.text.toString());
                    //开始倒计时
                    startCountdownTimer();
                  }
                },
                child: Text(
                  _countdownTime > 0 ? '$_countdownTime后重新获取' : '获取验证码',
                  style: TextStyle(
                    fontSize: 16,
                    color: _countdownTime > 0
                        ? Colors.blue
                        : Color.fromARGB(255, 17, 132, 255),
                  ),
                ),
              ),
            ],
          ),
        ));
  }

  void startCountdownTimer() {
    print(_phoneCode.text.toString().length);
    if(_phoneCode.text.toString().length < 5){
      BrnToast.show("手机号不能为空", context);
    }else{
      _timer = Timer.periodic(
          Duration(seconds: 1),
              (Timer timer) => {
            setState(() {
              if (_countdownTime < 1) {
                //widget.onTimerFinish(); //倒计时结束后的回调
                _timer.cancel();
              } else {
                _countdownTime = _countdownTime - 1;
              }
            })
          });
    }

  }

  @override
  void dispose() {
    super.dispose();
    if (_timer != null) {
      _timer.cancel();
    }
  }
}
